package LawsonTransformJava.pgm;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Properties;

import LawsonTransformJava.dao.DAOFactoryOracleDB;
import LawsonTransformJava.dao.DAOHREmpUsf;
import LawsonTransformJava.dao.DAObject;
import LawsonTransformJava.data.TblHREmpUsf;


public class HR511HREmpUsf extends DAObject implements DAOHREmpUsf {

	public HR511HREmpUsf() {
	    // initialization 
			try {
				// There is no separate output file for PAEmployees.
				// It is assigned to an appropriate Employee record.
				this.output = null;
				init();
			} catch (ClassNotFoundException e) {
			    // Could not find the database driver
				System.out.println("ClassNotFoundException: " + e);
			} catch (SQLException e) {
			    // Could not connect to the database
				System.out.println("SQLException: " + e);
			} catch (IOException e) {
				// Could no write properties file
				System.out.println("IOException: " + e);
			}
	}
	
	public TblHREmpUsf findEmployee(String employeeNumber) {
		TblHREmpUsf heu = new TblHREmpUsf();
		try {
			whereClause = " WHERE EMPLOYEE='" + employeeNumber + "'";
			//FIELD_KEY=99 is the Employment Type User Field
			whereClause = whereClause + " AND FIELD_KEY=99";
			sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME + (whereClause==null?"":whereClause);
			rslt = stmtData.executeQuery(sqlData);
			rsmd = rslt.getMetaData();
			rslt.next();
			return (TblHREmpUsf) createObject(rslt, heu);
			//createFields(emp);
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
		return new TblHREmpUsf();
	}

	public TblHREmpUsf findEmployee(int company, String employeeNumber) {
		TblHREmpUsf heu = new TblHREmpUsf();
		try {
			whereClause = " WHERE COMPANY=" + company + " " + " AND EMPLOYEE='" + employeeNumber + "'";
			//FIELD_KEY=99 is the Employment Type User Field
			whereClause = whereClause + " AND FIELD_KEY=99";
			sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME + (whereClause==null?"":whereClause);
			rslt = stmtData.executeQuery(sqlData);
			rsmd = rslt.getMetaData();
			rslt.next();
//			return createPAEmployee(rslt);
			heu = (TblHREmpUsf) createObject(rslt, heu);			
			//createFields(emp);
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
		return new TblHREmpUsf();
	}

	public boolean hasMore () {
	  return hasMoreRecs;
	}

		@Override
	public int countHREmpUsfs() {
		try {
			countRecs = stmtData.executeQuery("SELECT COUNT(*) FROM " + data.getSchema() + "." + TABLE_NAME + (whereClause==null?"":whereClause));
			countRecs.next();
			return recCount=countRecs.getInt(1);
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		}
		return 0;
	}

	  private void init() throws SQLException, ClassNotFoundException, IOException {
		    // Set the data and xref variables
		    data = DAOFactoryOracleDB.getDAOOracleDB(true);
//		    xref = new DAOFactoryAccessDB();
		    sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME;
		    stmtData = data.getDataStatement();	    
		    countStmtData = data.getCountStatement();
		    //Set the Date Format for output
		    df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
	  }
}